#!/usr/bin/perl

use strict;
use warnings;
use lib 'lib/';
use lib '../SQL-Builder/lib/';

use Data::Dumper;
use MySQL::Query;
use MySQL::Query::Hints;
use MySQL::Query::Tables;
use MySQL::Query::Extract;
use MySQL::Query::OrderOfExecution;

use MySQL::Indexes;
my $i = MySQL::Indexes->new(dbh => $dbh);


my $simple_sql = q{
    SELECT row FROM table JOIN table2 ON table.id = table3.id JOIN table3 USE INDEX(id) ON table2.id = table3.id
};

print $simple_sql , "\n";

my $hints = MySQL::Query::Hints->new();

my $st = $hints->add_straight_join($simple_sql);
print $st , "\n";

my $kl = $hints->delete_straight_join($st);
print $kl , "\n";

my $up = $hints->add_use_index($simple_sql , 'table' , [qw(in1 in2)]);
print $up , "\n";

my $q = $hints->delete_use_index($up , 'table3');

print $q , "\n";

die;

my $sql = q{
    SELECT table1.id1 , table2.id2
    FROM table1 JOIN table2 USE INDEX(id1) ON t1.id = t2.id WHERE table2.id != '10' AND NOT (table2.ll = "23" OR table1.dd = "43")
};

my $tree = MySQL::Query->new(sql => $sql);

#print Dumper $tree->tree;

my $tables = MySQL::Query::Tables->new(tree => $tree);

#print "\n\t as_builder() \n";
print $tree->as_builder->sql;
                    print "\n \n";

my $table_names = $tables->tables;
#print "Table list: " , join " " , @$table_names , "\n";
my $aliases_list = $tables->aliases;
#print "Aliases list: " , join " " , @$aliases_list , "\n";
#print "------------------ \n";

my $uniq = $tables->uniq;

my $extract = MySQL::Query::Extract->new( tree => $tree , tables => $tables );

print "\n ---------------------------- \n";

foreach my $u (@$uniq) {
        my $builder = $extract->extract_one($u);
        print $builder->sql;
        print "\n ---------------------------- \n";
}

#print $extract->extract('main' , 'q')->sql;

#die;

#while (my $table_name = $tables->next()) {
#       print "Table: " , $table_name , "\n";
#       my $alias = $tables->alias;
#       if ($alias) {
#            while (my $alias_name = $alias->next) {
#                   print "\t-alias: " , $alias_name , "\n";
#                   my $builder = $extract->extract_one($table_name , $alias_name);
#                   print "" , $builder->sql , "\n";
#            }

#       } else {
#            my $builder = $extract->extract_one($table_name);
#            print "" , $builder->sql , "\n";
#       }
#       print "------------------\n";
#}

#print "\n-----------------------------------------------\n";


my $s = $extract->extract_all;
print "\n-----------------------------------------------\n";

#use Data::Dumper;
#print Dumper $s;

print $s->sql;

print "\n-----------------------------------------------\n \n";
my $ooe = MySQL::Query::OrderOfExecution->new();
my $dif_sql = $ooe->orders($s , $tables);



for (@$dif_sql) {
       print $_ , "\n";
       print "-----------------------------\n";
}

print "\tTOTAL: " , $#{$dif_sql} + 1 , "\n";